﻿#include "book_sql.h"

#include <QMessageBox>
#include <QCoreApplication>
#include <QSqlQuery>
#include <QtDebug>
#include <QSqlError>
#include <QDir>
book_sql::book_sql(QObject *parent)
    :QObject{parent}
{
    bok_init();
}
void book_sql::bok_init()
{
    qDebug()<<"book initializing begin";
    if(QSqlDatabase::drivers().isEmpty())
            QMessageBox::information(nullptr,tr("No database drvers found"),tr("This demo requires at least one Qt database driver."
                                                                          "Please check the documentation how to build the "
                                                                          "Qt SQL plugins."));
       book_db = QSqlDatabase::addDatabase("QSQLITE");
       book_db.setDatabaseName("C:\\LIBDB\\data.db");

       if(!book_db.open())
            QMessageBox::warning(nullptr,tr("Unable to open database"),tr("An error occurred while opening book_data"));

    qDebug()<<"book initializing end";
}
 bool book_sql::add_book(book_info boOOk)
 {
     QSqlQuery sql(book_db);
     QString strSql=QString("INSERT INTO book_info values('%1','%2','%3','%4','%5','%6','%7','%8','%9')").
             arg(boOOk.book).
             arg(boOOk.borrow_left).
             arg(boOOk.borrow_allow).
             arg(boOOk.author).
             arg(boOOk.page).
             arg(boOOk.borrow_ID).
             arg(boOOk.store_place).
             arg(boOOk.borrow_date).
             arg(boOOk.borrow_num);
     qDebug()<<"add_book function working "<<strSql;
     sql.exec(strSql);
     return true;
 }
